<template>
  <div class="main-layout">
    <el-card class="box-card-list" shadow="never">
      <div class="text-large-up-26">
        通知公告
      </div>

      <div class="content-list" v-loading="loading">
        <div class="list-item" 
          v-for="(item, index) in noticeList" 
          :key="item.notice_id"
          @click="goDetails(item.notice_id)"
          :class="{
            'margin-space': true,
            'list-height': true,
            'list-select': true,
            'odd-item': index % 2 === 0,
            'even-item': index % 2 !== 0,
          }"
        >
          <div class="text-large">{{ item.title }}</div>
          <div class="text-large">{{ formatDate(item.publish_time) }}</div>
        </div>
      </div>

      <div class="example-pagination-block" v-if="total > 0">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 30, 40]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import NoticeApi from '@/api/notice'
import type { NoticeData } from '@/api/notice/types'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loading = ref(true)
const noticeList = ref<NoticeData[]>([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 格式化日期
const formatDate = (dateStr?: string) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 获取通知列表
const getNoticeList = async () => {
  try {
    loading.value = true
    const response = await NoticeApi.getNotices({
      Page: currentPage.value,
      Size: pageSize.value,
      is_delete: 0,
      status: 1
    })
    
    // 直接使用返回的数组数据
    if (Array.isArray(response.data)) {
      noticeList.value = response.data
      total.value = response.data.length  // 或者使用后端返回的总数
    } else {
      noticeList.value = []
      total.value = 0
      console.log('Invalid data format')
    }
  } catch (error) {
    ElMessage.error('获取通知列表失败')
    console.error('获取通知列表失败:', error)
    noticeList.value = []
    total.value = 0
  } finally {
    loading.value = false
  }
}

// 处理页码变化
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getNoticeList()
}

// 处理每页数量变化
const handleSizeChange = (val: number) => {
  pageSize.value = val
  currentPage.value = 1
  getNoticeList()
}

// 跳转详情页
const goDetails = (noticeId: number) => {
  router.push({
    name: 'noticeDetail',
    params: { id: noticeId }
  })
}

// 组件挂载时获取通知列表
onMounted(() => {
  getNoticeList()
})
</script>

<style scoped>
.content-list {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px 0;
}

.list-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  transition: all 0.3s;
}

.list-height {
  height: 50px;
  line-height: 50px;
}

.list-select {
  cursor: pointer;
}

.odd-item {
  background-color: #fff;
}

.even-item {
  background-color: #f5f7fa;
}

.margin-space {
  margin: 10px 50px;
}

.example-pagination-block {
  display: flex;
  justify-content: center;
  margin: 20px 0;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .content-list {
    padding: 10px;
  }

  .margin-space {
    margin: 5px 20px;
  }

  .list-height {
    height: 40px;
    line-height: 40px;
  }
}
</style>